{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f83c7034",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在Python中只要对一些内容加上对双引号或一对单引号，就默认为是字符串；（三对单引号或三对双引号可以写多行字符串）\n",
    "\n",
    "# 字符串的关键是str，是string的缩写。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4fc7f0ff",
   "metadata": {},
   "outputs": [],
   "source": [
    ">>># 定义字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8185a42c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (3614027496.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [5]\u001b[1;36m\u001b[0m\n\u001b[1;33m    >>>name = \"小红Tos\"\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    ">>> name = \"小红Tos\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f9bc9663",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "EOF while scanning triple-quoted string literal (1786077341.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [6]\u001b[1;36m\u001b[0m\n\u001b[1;33m    na = \"\"\"小\u001b[0m\n\u001b[1;37m             ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m EOF while scanning triple-quoted string literal\n"
     ]
    }
   ],
   "source": [
    ">>> na = \"\"\"小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "661f4ea0",
   "metadata": {},
   "outputs": [],
   "source": [
    "...小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90f92dde",
   "metadata": {},
   "outputs": [],
   "source": [
    "...sn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5a70239",
   "metadata": {},
   "outputs": [],
   "source": [
    "...\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2d9c7ef",
   "metadata": {},
   "outputs": [],
   "source": [
    ">>>me = str(111)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3dc0c47d",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (1949221178.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [7]\u001b[1;36m\u001b[0m\n\u001b[1;33m    >>>print(type(name),type(na),type(me))\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    ">>>print(type(name),type(na),type(me))\n",
    "<class'str'><class'str'><class'str'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f680933",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在Python中，只要是被一对或三对双引号和单引号包起来的内容都是字符串；三对引号可以定义多行字符串；也可以使用字符串的关键字str来定义字符串；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "945a6464",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串的取值：\n",
    "\n",
    "获取字符串中的第1个字符：x[0]；\n",
    "\n",
    "获取字符串的最后1个字符：x[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c096af32",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character '：' (U+FF1A) (3985531940.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [8]\u001b[1;36m\u001b[0m\n\u001b[1;33m    * 字符串的切片：\u001b[0m\n\u001b[1;37m            ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character '：' (U+FF1A)\n"
     ]
    }
   ],
   "source": [
    "# 字符串的切片：\n",
    "\n",
    "切片是从一个字符串中取出一个子字符串；\n",
    "\n",
    "字符串的下标取值，是前闭后开的，如：x[0:3]，表示获取字符串中的0、1、2共3个字符，下标为3字符是不会被获取的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22d2af66",
   "metadata": {},
   "outputs": [],
   "source": [
    "# count函数用于统计字符串里某个子字符串出现的次数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10845de2",
   "metadata": {},
   "outputs": [],
   "source": [
    "tr = 'abcabcd'\n",
    "print('统计单个字符出现的次数：', str.count('a'))\n",
    "print('统计单个字符出现的次数：', str.count('a', 1))\n",
    "print('统计单个字符出现的次数：', str.count('a', 1, 3))\n",
    "print('统计子字符串出现的次数：', str.count('ab'))\n",
    "print('统计子字符串出现的次数：', str.count('df'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
